package com.liu.efk.zdcq.sevice;

import com.liu.efk.zdcq.pojo.ClientManager;
import com.liu.efk.zdcq.pojo.CollectPojo;
import com.liu.efk.zdcq.socket.WebSocket;
import com.liu.efk.zdcq.util.DateTimeUtil;
import com.liu.efk.zdcq.util.TaskIDGen;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



/**
 * Created by shi860715@126.com
 * Date 2021/5/19 10:58
 * Description
 */
@Service
@Slf4j
public class CollectService {

    @Autowired
    WebSocket webSocket;


    public String sendcommd(String dataID, CollectPojo request) {

        System.out.println("================================");

        Channel channel = ClientManager.getClientChannel(request.getAddress());
        if(channel==null){
            return "设备不在线";
        }
        TaskIDGen taskIDGen = TaskIDGen.getInstance();
        String id = taskIDGen.getTaskId();
        log.info(id);
        String task="";
        switch (Integer.parseInt(dataID)){
            case 1:
                task="###<TaskID="+id+";TaskFlag=1;TaskContent{ObjectAddress="+request.getAddress()+";CommandStyle=2;DataID=1;DataContent<ResetFlg="+request.getResetFlg()+">}>###";
                break;
            case 2: //设置参数
                task="###<TaskID="+id+";TaskFlag=1;TaskContent{ObjectAddress="+request.getAddress()+";CommandStyle=2;DataID=2;DataContent<RunCycle="+request.getRunCycle()+";TemperatureLimit="+request.getTemperatureLimit()+";SoakingTime="+request.getSoakingTime()+";LightLimit=1;StartTime=06:11;StopTime=17:15;SleepStartDate=10-01;SleepStopDate=04-11;CleanWormCycle=1;HeartBeatCycle=100>}>###";
                break;
            case 3:
                task="###<TaskID="+id+";TaskFlag=1;TaskContent{ObjectAddress="+request.getAddress()+";CommandStyle=2;DataID=3;DataContent<>}>###";
                break;
                case 4: //读取软件版本号
                task="###<TaskID="+id+";TaskFlag=1;TaskContent{ObjectAddress="+request.getAddress()+";CommandStyle=2;DataID=4;DataContent<VersionType="+request.getVersionType()+">}>###";
                break;
            case 5: // 校对时间
                task="###<TaskID="+id+";TaskFlag=1;TaskContent{ObjectAddress="+request.getAddress()+";CommandStyle=2;DataID=5;DataContent<DateTime="+ DateTimeUtil.getTodayChar19()+">}>###";
                break;
            case 6: //召测时钟
                task="###<TaskID="+id+";TaskFlag=1;TaskContent{ObjectAddress="+request.getAddress()+";CommandStyle=2;DataID=6;DataContent<>}>###";

                break;
            case 7: // 读取自检日志
                task="###<TaskID="+id+";TaskFlag=1;TaskContent{ObjectAddress="+request.getAddress()+";CommandStyle=2;DataID=7;DataContent<>}>###";
                break;
            case 8: //读取传感器数据
                task="###<TaskID="+id+";TaskFlag=1;TaskContent{ObjectAddress="+request.getAddress()+";CommandStyle=2;DataID=8;DataContent<>}>###";
                break;
            case 9:// 拍照
                task="###<TaskID="+id+";TaskFlag=1;TaskContent{ObjectAddress="+request.getAddress()+";CommandStyle=2;DataID=9;DataContent<CamWay="+request.getWay()+">}>###";
                break;
        }
        log.info("发送消息为："+task);
        channel.writeAndFlush(task);



        return task;
    }





}
